//
//  NYTabBar.m
//  百思不得姐
//
//  Created by Apple on 15/8/22.
//  Copyright (c) 2015年 Neal. All rights reserved.
//

#import "NYTabBar.h"
#import "NYPublishViewController.h"

@interface NYTabBar ()
@property(nonatomic ,weak) UIButton *publishButton;
@end
@implementation NYTabBar
//新增发布按钮
- (UIButton *)publishButton
{
    if (!_publishButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
        [button sizeToFit];
        [button addTarget:self action:@selector(publishClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        _publishButton = button;
    }
    return _publishButton;
}
- (void)publishClick
{
    NYPublishViewController *publish = [[NYPublishViewController alloc]init];
    [self.window.rootViewController presentViewController:publish animated:NO completion:^{
        
    }];

}
//添加发布按钮到 tabbar 中
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.publishButton.centerX = self.width  * 0.5;
    self.publishButton.centerY = self.height * 0.5;
    
    CGFloat buttonW = self.width/5;
    int i = 0;
    for (UIView *tabBarButton in self.subviews) {
        //判断是否为发布按钮，将发布按钮插到中间
        if (![NSStringFromClass(tabBarButton.class) isEqualToString:@"UITabBarButton"]) {
            continue;
        }
        tabBarButton.width = buttonW;
        tabBarButton.x = i *buttonW;
        if (i>1) {
            tabBarButton.x += buttonW;
        }
        i++;
    }
}

@end
